import { defineStore } from "pinia";
import { useCookies } from "vue3-cookies";

import { login, register } from "@/api/auth";
import { getUnprocessedList } from "@/api/product";
import { getUserAssets, getUserInfo } from "@/api/uc";
import { AuthType } from "@/layout/main-layout/AuthModal/authModal";

interface AuthStore extends API.Auth.LoginResponse {
  login: {
    toggle: boolean;
    type: AuthType;
  };
  user_balance: string;
  unProcessedNum: number;
  userInfo: API.UC.UserInfoDetailResponse;
}

const TOKEN_KEY = "token";
const USER_ID = "REALBOX_USER_ID";

export const useAuth = defineStore("auth", {
  state: (): AuthStore => ({
    login: {
      toggle: false,
      type: AuthType.Login,
    },
    token: void 0,
    user_id: void 0,
    user_balance: "",
    unProcessedNum: 0,
    userInfo: {},
  }),
  getters: {
    userToken(state) {
      if (state.token) {
        return state.token;
      }
      return useCookies().cookies.get(TOKEN_KEY) || void 0;
    },
    userId(state) {
      if (state.user_id) {
        return state.user_id;
      }
      return localStorage.getItem(USER_ID)
        ? Number(localStorage.getItem(USER_ID))
        : void 0;
    },
  },
  actions: {
    onOpenLogin(type: AuthType) {
      this.login = {
        toggle: true,
        type,
      };
    },
    onOpenLoginPanel() {
      this.onOpenLogin(AuthType.Login);
    },
    onOpenRegisterPanel() {
      this.onOpenLogin(AuthType.Register);
    },
    async onLogin(params: API.Auth.LoginParams) {
      const result = await login(params);
      result && this.onLoginSuccess(result);
    },
    async onRegister(params: API.Auth.RegisterParams) {
      const result = await register(params);
      result && this.onLoginSuccess(result);
    },
    onLoginSuccess(params: API.Auth.LoginResponse) {
      this.token = params.token;
      this.user_id = params.user_id;
      useCookies().cookies.set(TOKEN_KEY, this.token);
      localStorage.setItem(USER_ID, this.user_id);
      this.login.toggle = false;
      this.getUserBasicInfo();
    },
    async getUserBasicInfo() {
      const request = [this.getUserInfo(), this.getAssetAndCard()];
      await Promise.all(request);
    },
    async getUserInfo() {
      const result = await getUserInfo();
      this.userInfo = result || {};
    },
    async getAssetAndCard() {
      const request = [this.getUserAssets(), this.getUnprocessedList()];
      await Promise.all(request);
    },
    async getUserAssets() {
      const result = await getUserAssets();
      this.user_balance = result?.user_balance || 0;
    },
    async getUnprocessedList() {
      const result = await getUnprocessedList({
        page: 1,
        size: 1,
      });
      this.unProcessedNum = result?.total || 0;
    },
    clear() {
      useCookies().cookies.remove("token", null, ".realboxs.com");
      useCookies().cookies.remove("token");
      localStorage.clear();
      this.token = void 0;
      this.user_id = void 0;
      this.userInfo = {};
      this.user_balance = "";
      this.unProcessedNum = "";
    },
  },
});
